//
//  HomeTableViewController.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/7/27.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    var statuses: [Status]? {
        didSet{
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !UserAccount.userLogin {
            visitorLoginView?.setupInfo("visitordiscover_feed_image_smallicon", message:"关注一些人，回这里看看有什么惊喜", isHome: true)
            return
        }
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectPicture:", name: TSYStatusCellPictureNotification, object: nil)
        
        //注册原型Cell
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusCellIdentifier.NormalCell.rawValue)
        tableView.registerClass(StatusForwardCell.self, forCellReuseIdentifier: StatusCellIdentifier.ForwardCell.rawValue)
        tableView.estimatedRowHeight = 300
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
 
        loadData()
    }
    //注销通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    @objc private func selectPicture(n:NSNotification) {
        
        guard let largePictureURLs = n.userInfo![TSYStatusCellSelectPictureURLKey] as? [NSURL] else{
            print("照片不存在")
            return
        }
        
        guard let indexPath = n.userInfo![TSYStatusCellSelectPictureIndexKey] as? NSIndexPath else {
            print("没有索引值")
            return
        }
     
    
        let vc = PictureBrowerViewController(urls: largePictureURLs, index: indexPath.item)
        
        presentViewController(vc, animated: true, completion: nil)
    }
    
    private func loadData() {
        Status.loadstatus {[weak self] (dataList, error) -> () in
            if error != nil {
                print(error)
                return
            }
            self!.statuses = dataList
        }
    }
    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let status = statuses![indexPath.row]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(StatusCellIdentifier.cellID(status), forIndexPath: indexPath) as! StatusCell
        cell.status = statuses![indexPath.row]
        return cell
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        //获取缓存行高
        let status = statuses![indexPath.row]
        
        if let h = status.rowHeight {
            return h
        }
        
        let cell = tableView.dequeueReusableCellWithIdentifier(StatusCellIdentifier.cellID(status)) as? StatusCell
 
        status.rowHeight =  cell!.rowHeight(status)
        return status.rowHeight!
        
    }
}
